using PageFlowQuickstart.Modules.Customers.BusinessEntities;
using PageFlowQuickstart.Modules.Customers.Tests.Mocks;
using PageFlowQuickstart.Modules.Customers.Views;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PageFlowQuickstart.Modules.Customers.Tests.Views.Mocks;

namespace PageFlowQuickstart.Modules.Customers.Tests.Views
{
    [TestClass]
    public class CustomerDetailsViewPresenterFixture
    {
        [TestMethod]
        public void OnViewInitializedCallsControllerCurrentCustomerAndViewSetCustomer()
        {
            MockCustomersController controller = new MockCustomersController();
            Customer customer = controller.InnerCurrentCustomer = new Customer();
            CustomerDetailsViewPresenter presenter = new CustomerDetailsViewPresenter(controller);
            MockApproveCustomerView view = new MockApproveCustomerView();
            presenter.View = view;
            
            presenter.OnViewInitialized();

            Assert.IsTrue(controller.CurrentCustomerRetrieved);
            Assert.IsTrue(view.CustomerSet);
            Assert.AreSame(customer, view.Customer);
        }

        [TestMethod]
        public void OnViewInitializedDisablesShowingAndApprovingCustomerIfControllerCurrentCustomerIsNull()
        {
            MockCustomersController controller = new MockCustomersController();
            controller.InnerCurrentCustomer = null;
            CustomerDetailsViewPresenter presenter = new CustomerDetailsViewPresenter(controller);
            MockApproveCustomerView view = new MockApproveCustomerView();
            presenter.View = view;

            presenter.OnViewInitialized();

            Assert.IsTrue(controller.CurrentCustomerRetrieved);
            Assert.IsNull(view.Customer);
            Assert.IsFalse(view.AllowApproveCustomer);
            Assert.IsFalse(view.ShowCustomerDetails);
        }

        [TestMethod]
        public void OnApproveCustomerCallsControllerApproveCurrentCustomer()
        {
            MockCustomersController controller = new MockCustomersController();
            CustomerDetailsViewPresenter presenter = new CustomerDetailsViewPresenter(controller);
            MockApproveCustomerView view = new MockApproveCustomerView();
            presenter.View = view;

            presenter.OnApproveCustomer();

            Assert.IsTrue(controller.ApproveCurrentCustomerCalled);
        }
    }

    namespace Mocks
    {
        internal class MockApproveCustomerView : ICustomerDetailsView
        {
            private Customer _customer = null;
            private bool _allowApproveCustomer = true;
            private bool _showCustomerDetails = true;
            public bool CustomerSet = false;

            #region IApproveCustomerView Members

            public bool AllowApproveCustomer
            {
                set { _allowApproveCustomer = value; }
                get { return _allowApproveCustomer; }
            }

            public Customer Customer
            {
                set 
                {
                    _customer = value;
                    CustomerSet = true; 
                }
                get { return _customer; }
            }

            public bool ShowCustomerDetails
            {
                set { _showCustomerDetails = value; }
                get { return _showCustomerDetails; }
            }

            #endregion
        }
    }
}
